/*
 * This program was write for catching  email addresses
 * from a document that contains lots of them.
 */
package charles.testproject.readfile;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


import charles.testproject.test.Test;

/**
 * @author Sancho_lai
 * 
 */
public class ReadFileFromLocalAndWeb  {

	public static void main(String args[]) {
		/**
		 * this part get email address from local doc
		 */
		System.out.println("********************Get Email Address From Local doc********************");
		try {
			/*
			 * here you can input whatever address of doc that contains the email addresses you want,
			 */
			BufferedReader br = new BufferedReader(new FileReader("doc\\EmailAddress.txt"));
//			BufferedReader br = new BufferedReader(new FileReader("E:\\workspace\\Project_For_ToyTest\\doc\\New OpenDocument Text.odt"));
			String line = "";
			
			while( (line = br.readLine()) != null) {
				parser(line);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		/**
		 * this part get email address from web
		 */
		System.out.println("********************Get Email Address From the Web********************");
		String line = "";
		InputStream urlStream;
		try {
			URL url = new URL("http://www.8885.net/bbs/thread-189746-1-3.html");
			HttpURLConnection connection = (HttpURLConnection)url.openConnection();
			urlStream = connection.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(urlStream,"gbk"));
			while( (line = br.readLine()) != null) {
				parser(line);
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
		}
	}

	private static void parser(String line) {
		//this is the email address pattern.
		Pattern p = Pattern.compile("[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+");
		Matcher m = p.matcher(line);
		while(m.find()) {
			System.out.println(m.group());
		}
	}
}
